package com.flot.yazb.home.setting;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BActivity;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.ChannelManage;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.model.PayModel;
import com.flot.yazb.model.other.PayStatusModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.home.moudle.pay.WebViewActivity;
import com.flot.yazb.widget.utils.IsNullOrEmpty;
import com.flot.yazb.widget.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayVipActivity extends BActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tltit)
    RelativeLayout tltit;
    @BindView(R.id.buy1)
    TextView buy1;
    @BindView(R.id.buy2)
    TextView buy2;
    @BindView(R.id.buy3)
    TextView buy3;
    @BindView(R.id.buy4)
    TextView buy4;
    @BindView(R.id.llpaywx)
    LinearLayout llpaywx;
    @BindView(R.id.llpayzfb)
    LinearLayout llpayzfb;
    private int payType=2;
    private String money;
    private String param;
    private PayStatusModel setmodel;
    private UserModel selfModel=BApplication.getInstance().getUserModel();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_vip);
        ButterKnife.bind(this);
        showProgress();
        PayStatusModel.getPsyStatus(new BModel.Transit<PayStatusModel>(this) {
            @Override
            public void onBorn(PayStatusModel serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK) && serializable != null) {
                    setmodel = serializable;
                    initUI();
                    if ("alipay".equals(serializable.first)){ //优先选择支付宝
                        payType=1;
                        llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
                        llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
                    }else if ("weixinpay".equals(serializable.first)){ // 选择微信
                        payType=2;
                        llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
                        llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
                    }else{
                        llpaywx.setVisibility(View.GONE);
                        llpayzfb.setVisibility(View.GONE);
                        buy1.setVisibility(View.GONE);
                        buy2.setVisibility(View.GONE);
                        buy3.setVisibility(View.GONE);
                        buy4.setVisibility(View.GONE);
                        payType=-1;
                    }

                }else{
                    ToastUtil.showToast(activity,message);
                    finish();
                }
            }
        });
    }
    private void initUI() {
        if (setmodel.alipay==1){
            llpayzfb.setVisibility(View.VISIBLE);
            llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
            llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
            payType=1;
        }else{
            llpayzfb.setVisibility(View.GONE);
            llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
            llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
            payType=2;
        }

        if (setmodel.weixinpay==1){
            llpaywx.setVisibility(View.VISIBLE);
            llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
            llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
            payType=2;
        }else{
            llpaywx.setVisibility(View.GONE);
            llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
            llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
            payType=1;
        }
    }


    public void intoPay() {
        showProgress();
        param="vip_"+ ChannelManage.getChannel(this);
        PayModel.getLink(payType+"", selfModel.id, money, param, new BModel.Transit<PayModel>(this) {
            @Override
            public void onBorn(final PayModel serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK)) {
                    if (serializable != null && serializable.pay_link != null){
                        if (IsNullOrEmpty.isEmpty(serializable.mode)) {
                            Intent intent = new Intent();
                            intent.setAction("android.intent.action.VIEW");
                            Uri content_url = Uri.parse(serializable.pay_link);
                            intent.setData(content_url);
                            startActivity(intent);
                            finish();
                        } else {
                            Intent intent = new Intent(PayVipActivity.this, WebViewActivity.class);
                            intent.putExtra("pay_type", payType + "");
                            intent.putExtra("money", money);
                            intent.putExtra("param", param);
                            intent.putExtra("link", serializable.pay_link);
                            startActivity(intent);
                            finish();
                        }

                    }
                } else {
                    ToastUtil.showToast(activity, message);
                }
            }
        });
    }

    @OnClick({R.id.buy1, R.id.buy2, R.id.buy3,R.id.buy4, R.id.llpaywx, R.id.llpayzfb,R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buy1:
                money=getResources().getString(R.string.payVip_1);
                intoPay();
                break;
            case R.id.buy2:
                money=getResources().getString(R.string.payVip_2_value);
                intoPay();
                break;
            case R.id.buy3:
                money=getResources().getString(R.string.payVip_3_value);
                intoPay();
                break;
            case R.id.buy4:
                money=getResources().getString(R.string.payVip_4_value);
                intoPay();
                break;
            case R.id.llpaywx:
                payType=2;
                llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
                llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
                break;
            case R.id.llpayzfb:
                payType=1;
                llpaywx.setBackground(getResources().getDrawable(R.drawable.solid_squ_gray_border));
                llpayzfb.setBackground(getResources().getDrawable(R.drawable.solid_squ_red_border));
                break;
            case R.id.iv_back:
                finish();
                break;

        }
    }
}
